package com.lianqi.emcpframework.data.masker.stratgey;
import com.lianqi.emcpframework.data.masker.annotation.MaskData;
import com.lianqi.emcpframework.utils.StringUtils;

/**
 * @program emcp-framework
 * @ClassName MoneyStrategy
 * @description: 金额脱敏策略实现类
 * @author: sky
 * @create: 2019/05/26 19:29
 */
public class MoneyStrategy implements DataMaskStrategy<String> {

    @Override
    public int defaultMaskLeft() {
        return 0;
    }

    @Override
    public int defaultMaskRight() {
        return 0;
    }


    @Override
    public String maskValue(Object value, MaskData maskData) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }
        int maskleft = 0;
        String oldValue = value.toString();

        int index = oldValue.indexOf(".");
        if (oldValue.startsWith("$") || oldValue.startsWith("￥")) {
            maskleft += 1;
        }
        if (index <= 1) {
            return org.apache.commons.lang3.StringUtils.rightPad(
                    org.apache.commons.lang3.StringUtils.left(oldValue, maskleft), index, "*").
                    concat(org.apache.commons.lang3.StringUtils.rightPad(".",
                            oldValue.length() - index - 1, "*"));
        }
        return org.apache.commons.lang3.StringUtils.rightPad(
                org.apache.commons.lang3.StringUtils.left(oldValue, maskleft), oldValue.length() - maskleft, "*");

    }

}
